package com.shuiqinling.ww.android.Util;

import java.util.HashMap;

import com.tendcloud.tenddata.TDGAAccount;
import com.tendcloud.tenddata.TDGAItem;
import com.tendcloud.tenddata.TDGAVirtualCurrency;

/**
 * 2017.02.08
 * 
 * @author LSN Talking Data的帮助类
 */
public class TDGAHelper {

	public static final int EVENT_TYPE_LOGIN = 0; // 登录事件(userId,username,gameserver)
	public static final int EVENT_TYPE_REGISTER = 1; // 注册事件(userId,username)
	public static final int EVENT_TYPE_CHANGE_ACC = 2; // 切换账户
	public static final int EVENT_TYPE_LEVEL = 3; // 升级(level)

	public static final int EVENT_TYPE_CHARGE_REQUEST = 4; // 充值请求(orderId, itemname, price,amount,paymentType)
	public static final int EVENT_TYPE_CHARGE_SUCCESS = 5; // 充值成功(orderId)
	public static final int EVENT_TYPE_REWARD = 6; // 获赠金币(amount,reason)

	public static final int EVENT_TYPE_ITEM_PURCHASE = 7; // 物品购买(itemname,amount,price)
	public static final int EVENT_TYPE_ITEM_USE = 8; // 物品消耗(item,amount)

	public static final int EVENT_TYPE_MISSION_BEGIN = 9; // 任务开始(missionId)
	public static final int EVENT_TYPE_MISSION_COMPLETE = 10;// 任务完成(missionId)
	public static final int EVENT_TYPE_MISSION_FAILED = 11; // 任务失败(missionId,reason)

	private static TDGAAccount acc;

	public static void processEvent(int eventType, HashMap<String, String> paramMap) {
		
		switch (eventType) {
		case EVENT_TYPE_LOGIN:
			
			String userId=paramMap.get("userId");
			String username=paramMap.get("username");
			String gameserver=paramMap.get("gameserver");
			doLogin(userId,username,gameserver);
			break;
		case EVENT_TYPE_REGISTER:
			userId = paramMap.get("userId");
			username = paramMap.get("username");
			doRegister(userId,username);
			break;
		case EVENT_TYPE_CHANGE_ACC:
			// doChangeAcc(param)
			break;
		case EVENT_TYPE_LEVEL:
			acc.setLevel(Integer.parseInt(paramMap.get("level")));
			break;
		case EVENT_TYPE_CHARGE_REQUEST:
			String orderId=paramMap.get("orderId");
			String itemname=paramMap.get("itemname");
			double price = Double.parseDouble(paramMap.get("price")); 
			int amount = Integer.parseInt(paramMap.get("amount"));
			String paymentType = paramMap.get("paymentType");
			TDGAVirtualCurrency.onChargeRequest(orderId,itemname,price,"CNY",amount,paymentType);
			break;
		case EVENT_TYPE_CHARGE_SUCCESS:
			TDGAVirtualCurrency.onChargeSuccess(paramMap.get("orderId"));
			break;
		case EVENT_TYPE_REWARD:
			double gold_amount = Double.parseDouble(paramMap.get("amount"));
			String reason = paramMap.get("reason");
			TDGAVirtualCurrency.onReward(gold_amount, reason);
			break;
		case EVENT_TYPE_ITEM_PURCHASE:
			itemname = paramMap.get("itemname");
			amount = Integer.parseInt(paramMap.get("amount"));
			price = Double.parseDouble(paramMap.get("price"));
			TDGAItem.onPurchase(itemname, amount, price);
			break;
		case EVENT_TYPE_ITEM_USE:
			itemname = paramMap.get("itemname");
			amount = Integer.parseInt(paramMap.get("amount"));
			TDGAItem.onUse(itemname, amount);
			break;
		case EVENT_TYPE_MISSION_BEGIN:

			break;
		case EVENT_TYPE_MISSION_COMPLETE:

			break;
		case EVENT_TYPE_MISSION_FAILED:

			break;
		default:
			break;
		}
	}




	private static void doRegister(String userId,String username) {
		acc = TDGAAccount.setAccount(userId);
		acc.setAccountName(username);
		acc.setAccountType(TDGAAccount.AccountType.REGISTERED);
	}

	private static void doLogin(String userId,String username,String gameserver) {
		acc = TDGAAccount.setAccount(userId);
		acc.setAccountName(username);
		acc.setGameServer(gameserver);
	}

}
